/*
 * Copyright (C) 2010 Seapeak.Xu
 *
 * fostmq may be copied only under the terms of the GNU General
 * Public License V3, which may be found in the fostmq source kit.
 *
 * Created on: 2010-10-13
 * Author: Seapeak.Xu
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>

#include "fost_notifypool.h"
#include "fost_thread.h"
#include "fost_logger.h"
#include "fost_mem.h"

int notifypool_init(size_t size,notify_read_func callback)
{

	int result = 0;
	if (0 == size)
	{
		log_error("File:%s,Line:%d,Function:%s.the notify pool size is 0.",
				__FILE__, __LINE__, __FUNCTION__);
		return -1;
	}

	g_notify_elts = (notify_elt_t *) fost_calloc(size,sizeof(notify_elt_t));
	if(NULL == g_notify_elts)
	{
		log_error("File:%s,Line:%d,Function:%s.malloc memory for notifypool list is error.Errno:%d,Info:%s",
				__FILE__,__LINE__,__FUNCTION__,errno,strerror(errno));
		return -1;
	}	
	do
	{
		int i;
		int end_postion = size - 1;
		for (i = end_postion; i >= 0; i--)//Descending
		{
			(g_notify_elts + i)->ev_base = event_base_new();
			(g_notify_elts + i)->tid = 0;
			(g_notify_elts + i)->idx = i;
			if(-1 == pipe((g_notify_elts + i)->pipe_fds))
			{
				log_error("File:%s.Line:%d,Function:%s.open the pipe is error.Error:%d,Info:%s.",
						__FILE__,__LINE__,__FUNCTION__,errno,strerror(errno));
				result = -1;
				break;
			}
			(g_notify_elts + i)->func = callback;
		}		
		if(0 == result)	return 0;			
	}while(false);
	FOST_FREE(g_notify_elts);				
	return result;

}

int notifypool_destroy()
{
	return 0;
}


//fucking,i do the work only,
//i donot know what ARCH group master to do.
//i just do,what are you?
